package inet.app.view.fragment;

import inet.app.R;
import inet.app.controller.database.DatabaseManager;
import inet.app.model.object.KqGiai_Item;
import inet.app.model.util.MyPreferenceManager;
import inet.app.model.util.SmartLog;
import inet.app.view.activity.KetQuaFragment;
import inet.app.view.activity.KetQuaFragment.ChangePagerListener;
import inet.app.view.activity.MyApp;

import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageButton;

public abstract class BaseFragmentKQ extends Fragment implements
		ChangePagerListener {
	public String MARK_COLOR = "\"#ADDFFF\">";
	protected KetQuaFragment mActivity;
	protected MyApp mApplication;
	protected DatabaseManager mDbManager;
	protected MyPreferenceManager mPreference;
	protected String mCode;
	protected MediaPlayer media;
	protected boolean dangnghe = false;
	protected ImageButton imgPlay;
	protected String date_current;
	protected int position = 0;
	protected String matinh = "";
	protected WebView webView;
	protected boolean isFree;
	protected ViewGroup root;
	private View mLoadingLayout;
	protected ArrayList<String> listaudio = new ArrayList<String>();
	protected List<KqGiai_Item> itemsReading;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		mActivity = (KetQuaFragment) getActivity();

		mActivity.addChangePageListener(this);

		mApplication = (MyApp) getActivity().getApplication();

		mDbManager = DatabaseManager
				.instance(mActivity.getApplicationContext());

		mPreference = MyPreferenceManager.getInstance(mActivity
				.getApplicationContext());

		isFree = mPreference.isFree();
	}

	protected void pauseAudio() {
		if (media != null && media.isPlaying()) {
			// if(!media.isPlaying()) position =0;
			mPreference.setPosAudio(mCode, matinh, date_current, position);
			SmartLog.d("pause at " + position);
			media.pause();
			dangnghe = false;
			imgPlay.setBackgroundResource(R.drawable.button_on_sound);
		}
	}

	protected void resumeAudio() {
		if (media != null) {
			position = mPreference.getPosAudio(mCode, matinh, date_current);
			SmartLog.d("reading position " + position);
			dangnghe = true;
			imgPlay.setBackgroundResource(R.drawable.button_off_sound);
			media.start();
		}
	}

	protected void resetAudio() {
		pauseAudio();
		if (media != null) {
			media.reset();
		}
		position = 0;
	}

	protected void stopAudio() {
		pauseAudio();
		if (media != null) {
			media.stop();
		}
		position = 0;
	}

	@Override
	public void onPause() {
		super.onPause();
		pauseAudio();
	}

	@Override
	public void onStop() {
		super.onStop();
		media.stop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		media.release();
		media = null;
	}

	@Override
	public void onChange(int page) {
		pauseAudio();
	}

	/**
	 * Show/hide view loading
	 * 
	 * @param isShow
	 */
	public void showLoading(boolean isShow) {
		if (mLoadingLayout == null) {

			// create view for loading
			mLoadingLayout = LayoutInflater.from(mActivity).inflate(
					R.layout.dialog_progress, null);
			if (root != null) {
				root.addView(mLoadingLayout);
			}
		}
		if (isShow) {
			mLoadingLayout.setVisibility(View.VISIBLE);
		} else if (mLoadingLayout != null) {
			mLoadingLayout.setVisibility(View.GONE);
		}
	}

	protected boolean isNumber(String fileName) {
		if (TextUtils.isEmpty(fileName)) {
			return false;
		}
		return fileName.contains("So_");
	}

	protected String getNumberFromPosition(int position) {
		int prevInd = -1;
		int nextInd = -1;
		int len = listaudio.size();
		if (position < 0 || position > len - 1) {
			return "";
		}
		for (int i = position; i >= 0; i--) {
			if (!isNumber(listaudio.get(i))) {
				prevInd = i + 1;
				break;
			}
		}
		for (int i = position; i < len; i++) {
			if (!isNumber(listaudio.get(i))) {
				nextInd = i;
				break;
			}
		}
		if (prevInd < 0 || nextInd < 0 || prevInd >= nextInd) {
			return "";
		}
		List<String> fileNames = listaudio.subList(prevInd, nextInd);
		// number type "voice/So/So_4.mp3"
		StringBuffer number = new StringBuffer();
		for (String fileName : fileNames) {
			int lenNumber = fileName.length();
			if (lenNumber > 5) {
				char digit = fileName.charAt(lenNumber - 5);
				number.append(digit);
			}
		}
		return number.toString();
	}

}
